package com.engine.salary.formlua.func.compare;

import com.engine.salary.formlua.entity.parameter.DataType;
import com.engine.salary.formlua.util.ExcelParamUtil;
import com.ql.util.express.instruction.op.OperatorMultiDiv;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.List;

/**
 * @className：除法重写类
 * @Description：注释
 * @Author：
 * @date：
 */
public class WOperatorDiv extends OperatorMultiDiv {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Object executeInner(Object[] list) throws Exception {
        DataType result = new DataType();
        result.setDataType(DataType.NUMBER);
        Object[] newList = new Object[2];
//		IgnoreParamFilter.checkNumberOper(list,"/");
        Object firstParam = ExcelParamUtil.getParamContent(list[0], "");
        Object secondParam = ExcelParamUtil.getParamContent(list[1], "");
        if (StringUtils.isEmpty(firstParam + "") || StringUtils.isEmpty(secondParam + "")) {
            result.setContent(0);
            return result;
        } else if (firstParam.toString().equalsIgnoreCase("0") || secondParam.toString().equalsIgnoreCase("0")) {
            result.setContent(0);
            return result;
        }
        if (firstParam instanceof Object[] || secondParam instanceof Object[]) {
            try {
                List<Object[]> operList = CommonOper.operDivMultiList(firstParam, secondParam, "/");
                Object[] results = new Object[operList.size()];
                for (int i = 0; i < operList.size(); i++) {
                    Object[] excuteArray = operList.get(i);
                    results[i] = super.executeInner(excuteArray);
                }
                return results;
            } catch (Exception e) {
                logger.error("err", e);
                return 0;
            }
        }
        newList[0] = firstParam;
        newList[1] = secondParam;
        try {
            Object r = super.executeInner(newList);
            result.setContent(new BigDecimal(r + ""));
        } catch (Exception e) {
            logger.error("除法计算异常返回0：" + e.getMessage());
            result.setContent(0);
        }
        return result;
    }

    public WOperatorDiv(String name) {
        super(name);
    }
}
